#include "7segments.h"
#define SEGMENTS_REGISTER 0x104
#define LED_A 0x1
#define LED_B 0x2
#define LED_C 0x4
#define LED_D 0x8
#define LED_E 0x10
#define LED_F 0x20
#define LED_G 0x40
#define MSB 0x80


int writeNumberToSegments(int num)
{
	if(num > 0xff || num < 0)
		return -1;
	int msDigit = num / 0x10;
	int lsDigit = num % 0x10;	
	writeDigitToSegments(msDigit,1);
	writeDigitToSegments(lsDigit,0);
	return 0;
}

int writeDigitToSegments(int dig, int msb)
{
	if(dig > 0xf || dig < 0)
		return -1;
	_sr(makeBits(dig) | (msb * MSB),SEGMENTS_REGISTER);
	return 0;
}

int makeBits(int dig)
{
	int ret = 0;
	switch(dig)
	{
	case 0:
		ret = LED_A | LED_B | LED_C | LED_D | LED_E | LED_F;
		break;
	case 1:
		ret = LED_B | LED_C;
		break;
	case 2:
		ret = LED_A | LED_B | LED_D | LED_E | LED_G;
		break;
	case 3:
		ret = LED_A | LED_B | LED_D | LED_C | LED_G;
		break;
	case 4:
		ret = LED_G | LED_B | LED_C | LED_F;
		break;
	case 5:
		ret = LED_A | LED_G | LED_C | LED_D | LED_F;
		break;
	case 6:
		ret = LED_A | LED_G | LED_C | LED_D | LED_E | LED_F;
		break;
	case 7: 
		ret = LED_A | LED_B | LED_C;
		break;
	case 8:
		ret = LED_A | LED_B | LED_C | LED_D | LED_E | LED_F | LED_G;
		break;
	case 9: 
		ret = LED_A | LED_G | LED_C | LED_B | LED_F;
		break;  
	case 0xa:
		ret = LED_A | LED_B | LED_C  | LED_E | LED_F | LED_G;
		break;
	case 0xb:
		ret = LED_C | LED_D | LED_E | LED_F | LED_G;
		break;
	case 0xc:
		ret = LED_A | LED_D | LED_E | LED_F;
		break;
	case 0xd:
		ret = LED_B | LED_C | LED_D | LED_E | LED_G;
		break;
	case 0xe:
		ret = LED_A | LED_D | LED_E | LED_F | LED_G;
		break;
	case 0xf:
		ret = LED_A | LED_E | LED_F | LED_G;
		break;
	default:
		ret = 0;
		break;
	}
	return ret;
}
